package cn.com.sports.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;

import cn.com.sports.model.File;
import cn.com.sports.model.User;
@Component
public class DB {
	private static Map<String,User> t_user = new HashMap<String,User>();
	private static Map<String,File> t_file = new HashMap<String,File>();
	public void saveOrUpdateUser(User user){
		t_user.put(user.getId(), user);
	}
	public void delUser(String id){
		t_user.remove(id);
	}
	public List<User> getAllUser(){
		List<User> users = new ArrayList<User>();
		Set<String> keys = t_user.keySet();
		for(String key : keys){
			users.add(t_user.get(key));
		}
		return users ;
	}
	public User getUserById(String id){
		return t_user.get(id);
	}
	public User getUserByUserOrEmail(String s){
		User user = null ;
		List<User> users = getAllUser();
		for(User u : users){
			if(s.equals(u.getEmail()) || s.equals(u.getUsername())){
				user = u ;
				break ;
			}
		}
		return user ;
	}
	public void saveFile(File file){
		t_file.put(file.getId(), file);
	}
	public List<File> getAllFile(){
		Set<String> files = t_file.keySet();
		List<File> flist = new ArrayList<File>();
		for(String f : files){
			flist.add(t_file.get(f));
		}
		return flist ;
	}
}
